package com.example.ouvaioufica;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


@SuppressLint("ParserError")
public class GameView extends View implements Runnable {

	private static final int MAX_SPEED = 5;
	private long INTERVAL = 1;
	private float cx;
	private float cy;
	Bitmap quadradinho;
	private Paint paint;
	private int iy2;
	private int ix2;
	private int w;
	private int h;
	private int dx;
	private int dy;
	private int pontos = 0;
	private long tempo = System.currentTimeMillis();
	
	public  GameView(Context context) {
		super(context);

		paint = new Paint();
		paint.setColor(Color.BLACK); 
		paint.setTextSize(20); 

		randomDirection();

		try {
			InputStream is = context.getAssets().open("quadros/icone.jpg");
			quadradinho = BitmapFactory.decodeStream(is);
			if (quadradinho != null) {
				ix2 = quadradinho.getWidth() / 2;
				iy2 = quadradinho.getHeight() / 2;
			}
		} catch (IOException e) {
			Log.e(MainActivity.TAG, "Erro carregando imagem");
		}

		Thread thread = new Thread(this);
		thread.start();
	}

	private void randomDirection() {
		Random r = new Random();
		dx = r.nextInt(MAX_SPEED) + 1;
		dy = r.nextInt(MAX_SPEED) + 1;
	}

	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);

		this.w = getWidth();
		this.h = getHeight();
		cx = w / 2;
		cy = h / 2;
	}

	public void draw(Canvas canvas) {
		super.draw(canvas);

		if (quadradinho != null) {
			canvas.drawBitmap(quadradinho, cx - ix2, cy - iy2, paint);
			paint.setColor(Color.BLACK);
			paint.setTextSize(20);
			canvas.drawText("Pontos: " + pontos, 0, 30, paint);
			int segundos = (int) (System.currentTimeMillis() - tempo)/1000;
			canvas.drawText("Tempo: " + segundos, 200, 30, paint);
			
		} else {
			Log.e(MainActivity.TAG, "Carai Fudeo");
		}
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(INTERVAL);
			} catch (InterruptedException e) {
				Log.e(MainActivity.TAG, "interrupcao do run()");
			}
			update();
			postInvalidate();
		}
	}

	private void update() {
		cx += dx;
		cy += dy;

		if (cx + ix2 > w || cx - ix2 < 0) {
			dx *= -1;
			cx += dx;
		}

		if (cy + iy2 > h || cy - iy2 < 0) {
			dy *= -1;
			cy += dy;
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		int mx = (int) event.getX();
		int my = (int) event.getY();
		int mw = quadradinho.getWidth();
		int mh = quadradinho.getHeight();
		
		if (action == MotionEvent.ACTION_DOWN) {
			randomDirection(); 
		}
		if(mx >= (cx - ix2) && mx <((cx - ix2) + mw) && my >= (cy - iy2) && my <((cy - iy2) + mh))
		{
			pontos++;
		}
		return super.onTouchEvent(event);
	}

}